package com.cxh.service.impl;

import com.cxh.base.Data2ServiceImpl;
import com.cxh.util.DataSource;
import com.cxh.dao.CarCompetitiveDao;
import com.cxh.dao.CarCompetitiveDetailDao;
import com.cxh.entity.CarCompetitive;
import com.cxh.entity.CarCompetitiveDetail;
import com.cxh.service.CarCompetitiveService;
import com.cxh.util.CommonUtility;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 类说明：学生表 
 * @author  YangHL
 * @date  2018-10-26 10:32:39
 */
@Service("carCompetitiveService")
@DataSource("dataSource2")
public class CarCompetitiveServiceImpl extends Data2ServiceImpl<CarCompetitive> implements CarCompetitiveService {
    @Autowired
    private CarCompetitiveDao dao;
    @Autowired
    private CarCompetitiveDetailDao carCompetitiveDetailDao;



    @Override
    public List<CarCompetitive> loadPageList(Map<String, Object> map) {
       return dao.loadPageList(map);
    }

    @Override
    public CarCompetitive getDataByCarCompetitive(CarCompetitive carCompetitiveSave) {
    	return dao.getDataByCarCompetitive(carCompetitiveSave);
    }
    
    @Override
    public void addData(CarCompetitive carCompetitiveSave) {
    	//插入竞品表
    	dao.insert(carCompetitiveSave);
    	
    	Integer id = carCompetitiveSave.getId();
    	CarCompetitiveDetail carCompetitiveDetail = new CarCompetitiveDetail();
    	carCompetitiveDetail.setCompetitiveid(id.toString());
    	
    	carCompetitiveDetail.setName(carCompetitiveSave.getCategoryname());
    	
    	//插入竞品详情表
    	carCompetitiveDetailDao.insert(carCompetitiveDetail);
    	carCompetitiveDetail.setName(carCompetitiveSave.getCompetitivecategoryname());
    	carCompetitiveDetail.setId(null);
    	//插入竞品详情表
    	carCompetitiveDetailDao.insert(carCompetitiveDetail);
    	
    }
    
    @Override
    public void updateData(CarCompetitive carCompetitiveSave) {
    	//竞品表的更新
    	dao.updateByPrimaryKey(carCompetitiveSave);
    	//拿着id查询竞品详情的信息
    	Integer id = carCompetitiveSave.getId();
    	
    	List<CarCompetitiveDetail> carCompetitiveDetailList = carCompetitiveDetailDao.getDataByCarCompetitiveId(id.toString());
    	CarCompetitiveDetail carCompetitiveDetail = carCompetitiveDetailList.get(0);
    	CarCompetitiveDetail carCompetitiveDetail2 = carCompetitiveDetailList.get(1);
    	
    	carCompetitiveDetail.setName(carCompetitiveSave.getCategoryname());
    	//竞品详情表的更新
    	carCompetitiveDetailDao.updateByPrimaryKey(carCompetitiveDetail);
    	
    	carCompetitiveDetail2.setName(carCompetitiveSave.getCompetitivecategoryname());
    	//竞品详情表的更新
    	carCompetitiveDetailDao.updateByPrimaryKey(carCompetitiveDetail2);
    }
    
    @Override
    public void deleteData(CarCompetitive carCompetitive) {
    	//先删除竞品详情的信息
    	carCompetitiveDetailDao.deleteByCarCompetitiveId(carCompetitive.getId().toString());
    	//再删除竞品的信息
    	dao.deleteByPrimaryKey(carCompetitive);
    }
}
